package com.jzz.eletuanbackend.mapper;

import com.jzz.eletuanbackend.entity.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CustomerMapper {

    @Select("SELECT * FROM v_shop_info WHERE id=#{id} LIMIT 1")
    CustomerShop shopBasicInfo(@Param("id") Long id);

    @Select("SELECT * FROM v_shop ORDER BY rate DESC")
    List<CustomerShop> allShops();

    // Todo 验证语句正确性
    @Select("SELECT b.* FROM shop_block AS a, block AS b WHERE a.shopId = #{id} AND a.blockId = b.id")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "commodities", column = "id",
                    many = @Many(select = "com.jzz.eletuanbackend.mapper.CustomerMapper.commoditiesOfBlock"))
    })
    List<Block> shop(@Param("id") Long id);

    @Select("SELECT b.* FROM shop_commodity AS a, commodity AS b WHERE a.blockId = #{bid} AND a.commodityId = b.id")
    List<Commodity> commoditiesOfBlock(@Param("bid") Long bid);

    @Select("SELECT b.shopId FROM shop_commodity AS a, shop_block AS b WHERE a.commodityId = #{cid} AND a.blockId = b.blockId LIMIT 1")
    Long getShopIdByCommodity(@Param("cid") Long cid);

    @Select("SELECT shipPrice FROM `shop` WHERE id = #{sid} LIMIT 1")
    Double getShipPrice(@Param("sid") Long sid);

    @Select("SELECT * FROM commodity WHERE id = #{id} LIMIT 1")
    Commodity getCommodity(@Param("id") Long id);

    @Update("UPDATE `customer` SET receiveInfo = #{inf} WHERE `id` = #{userId} LIMIT 1")
    Integer editReceiveInfo(@Param("userId") Long userId, @Param("inf") String inf);

    @Select("SELECT * FROM customer WHERE `id` = #{id} LIMIT 1")
    Customer info(@Param("id") Long userId);
}
